/*     */ package com.zimbra.cs.service.admin;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AccessManager.AttrRightChecker;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.AttributeManager.IDNType;
/*     */ import com.zimbra.cs.account.CalendarResource;
/*     */ import com.zimbra.cs.account.IDNUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ToXML
/*     */ {
/*     */   public static Element encodeAccount(Element parent, Account account)
/*     */   {
/*  40 */     return encodeAccount(parent, account, true, null, null);
/*     */   }
/*     */   
/*     */   public static Element encodeAccount(Element parent, Account account, boolean applyCos) {
/*  44 */     return encodeAccount(parent, account, applyCos, null, null);
/*     */   }
/*     */   
/*     */   public static Element encodeAccount(Element parent, Account account, boolean applyCos, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  49 */     return encodeAccount(parent, account, applyCos, false, reqAttrs, attrRightChecker);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static Element encodeAccount(Element parent, Account account, boolean applyCos, boolean needsExternalIndicator, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  56 */     Element acctElem = parent.addNonUniqueElement("account");
/*  57 */     acctElem.addAttribute("name", account.getUnicodeName());
/*  58 */     acctElem.addAttribute("id", account.getId());
/*     */     
/*  60 */     if (needsExternalIndicator) {
/*     */       try {
/*  62 */         boolean isExternal = account.isAccountExternal();
/*  63 */         acctElem.addAttribute("isExternal", isExternal);
/*     */       } catch (ServiceException e) {
/*  65 */         ZimbraLog.account.warn("unable to determine if account is external", e);
/*     */       }
/*     */     }
/*  68 */     Map attrs = account.getUnicodeAttrs(applyCos);
/*  69 */     encodeAttrs(acctElem, attrs, "n", reqAttrs, attrRightChecker);
/*  70 */     return acctElem;
/*     */   }
/*     */   
/*     */   public static Element encodeCalendarResource(Element parent, CalendarResource resource) {
/*  74 */     return encodeCalendarResource(parent, resource, false, null, null);
/*     */   }
/*     */   
/*     */   public static Element encodeCalendarResource(Element parent, CalendarResource resource, boolean applyCos) {
/*  78 */     return encodeCalendarResource(parent, resource, applyCos, null, null);
/*     */   }
/*     */   
/*     */   public static Element encodeCalendarResource(Element parent, CalendarResource resource, boolean applyCos, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  83 */     Element resElem = parent.addNonUniqueElement("calresource");
/*  84 */     resElem.addAttribute("name", resource.getUnicodeName());
/*  85 */     resElem.addAttribute("id", resource.getId());
/*  86 */     Map attrs = resource.getUnicodeAttrs(applyCos);
/*  87 */     encodeAttrs(resElem, attrs, "n", reqAttrs, attrRightChecker);
/*  88 */     return resElem;
/*     */   }
/*     */   
/*     */   public static void encodeAttrs(Element e, Map attrs, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker) {
/*  92 */     encodeAttrs(e, attrs, "n", reqAttrs, attrRightChecker);
/*     */   }
/*     */   
/*     */   public static void encodeAttrs(Element e, Map attrs, String key, Set<String> reqAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/*  97 */     encodeAttrs(e, attrs, key, reqAttrs, null, attrRightChecker);
/*     */   }
/*     */   
/*     */   public static void encodeAttrs(Element e, Map attrs, String key, Set<String> reqAttrs, Set<String> hideAttrs, AccessManager.AttrRightChecker attrRightChecker)
/*     */   {
/* 102 */     AttributeManager attrMgr = null;
/*     */     try {
/* 104 */       attrMgr = AttributeManager.getInstance();
/*     */     } catch (ServiceException se) {
/* 106 */       ZimbraLog.account.warn("failed to get AttributeManager instance", se);
/*     */     }
/*     */     
/* 109 */     Set<String> reqAttrsLowerCase = null;
/* 110 */     if (reqAttrs != null) {
/* 111 */       reqAttrsLowerCase = new HashSet();
/* 112 */       for (String reqAttr : reqAttrs) {
/* 113 */         reqAttrsLowerCase.add(reqAttr.toLowerCase());
/*     */       }
/*     */     }
/*     */     
/* 117 */     for (Iterator iter = attrs.entrySet().iterator(); iter.hasNext();) {
/* 118 */       Map.Entry entry = (Map.Entry)iter.next();
/* 119 */       String name = (String)entry.getKey();
/* 120 */       Object value = entry.getValue();
/*     */       
/*     */ 
/* 123 */       if (!name.equalsIgnoreCase("zimbraDataSourcePassword"))
/*     */       {
/*     */ 
/*     */ 
/* 127 */         value = Provisioning.sanitizedAttrValue(name, value);
/*     */         
/*     */ 
/* 130 */         if (((reqAttrsLowerCase == null) || (reqAttrsLowerCase.contains(name.toLowerCase()))) && (
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 135 */           (hideAttrs == null) || (!hideAttrs.contains(name))))
/*     */         {
/*     */ 
/*     */ 
/* 139 */           boolean allowed = attrRightChecker == null ? true : attrRightChecker.allowAttr(name);
/*     */           
/* 141 */           AttributeManager.IDNType idnType = AttributeManager.idnType(attrMgr, name);
/*     */           
/* 143 */           if ((value instanceof String[])) {
/* 144 */             String[] sv = (String[])value;
/* 145 */             for (int i = 0; i < sv.length; i++) {
/* 146 */               encodeAttr(e, name, sv[i], "a", key, idnType, allowed);
/*     */             }
/* 148 */           } else if ((value instanceof String)) {
/* 149 */             value = com.zimbra.cs.service.account.ToXML.fixupZimbraPrefTimeZoneId(name, (String)value);
/* 150 */             encodeAttr(e, name, (String)value, "a", key, idnType, allowed);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void encodeAttr(Element parent, String key, String value, String eltname, String attrname, AttributeManager.IDNType idnType, boolean allowed) {
/* 158 */     Element e = parent.addNonUniqueElement(eltname);
/* 159 */     e.addAttribute(attrname, key);
/*     */     
/* 161 */     if (allowed) {
/* 162 */       e.setText(IDNUtil.toUnicode(value, idnType));
/*     */     } else {
/* 164 */       e.addAttribute("pd", true);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/admin/ToXML.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */